#!/usr/bin/env bash

help(){
    cat<<__EOF__
Usage: m appearance [OPTIONS] [ARGUMENTS]

Description: Manage appearance settings on macOS.

Options:
  --help                                  Show this help message
  --antialiasthreshold [value]            Set the antialiasing threshold (e.g., 0, 2, 6, etc.).
                                          If you set it to 1, antialiasing will be disabled.
                                          If you dont pass a number, it will show the current value
  --darkmode <enable|disable>             Enable or disable dark mode
  --transparency <enable|disable>         Enable or disable transparency
  --iconsize <small|medium|large>         Set the size of the icons in window sidebars, dock, and finder
  --maincolor <blue|graphite>             Set the main color of the interface

  --highlightcolor <graphite  | cayenne    | asparagus  | clover
                  | teal      | midnight   | plum       | tin
                  | nickel    | mocha      | fern       | moss
                  | ocean     | eggplant   | maroon     | steel
                  | aluminum  | maraschino | lemon      | spring
                  | turquoise | blueberry  | magenta    | iron
                  | magnesium | tangerine  | lime       | seafoam
                  | aqua      | grape      | strawberry | tungsten
                  | silver    | salmon     | banana     | flora
                  | ice       | orchid     | bubblegum  | lead
                  | mercery   | cantaloupe | honeydew   | spindrift
                  | sky       | lavender   | carnation  | licorice
                  | snow>               Set the highlight color of the text selection
                                        in the interface.
__EOF__
exit 0
}


darkmode(){
    case $1 in
        enable)
            osascript -e \
                'tell application "System Events" to tell appearance preferences to set dark mode to true'
            ;;
        disable)
            osascript -e \
                'tell application "System Events" to tell appearance preferences to set dark mode to false'
            ;;
        *)
            current_mode=$(defaults read NSGlobalDomain AppleInterfaceStyle 2>/dev/null || echo "Light")
            echo "Appearance: ${current_mode}"
            ;;
    esac
}


# Helper function used in transparency options
show_trancparency_status(){
  local current
  current="$(defaults read com.apple.universalaccess ReduceTransparency 2>/dev/null || echo "false")"
  if [[ "$current" == "1" || "$current" == "true" ]]; then
    echo "Transparency: disabled"
  else
    echo "Transparency: enabled"
  fi
}

transparency(){
    case "${1}" in
        enable)
            defaults write com.apple.universalaccess ReduceTransparency -bool false
            echo "Transparency enabled"
            ;;
        disable)
            defaults write com.apple.universalaccess ReduceTransparency -bool true
            echo "Transparency disabled"
            ;;
        *)
            show_trancparency_status
            ;;
    esac
}


show_antialiasthreshold_status(){
  local current="$(defaults read NSGlobalDomain AppleAntiAliasingThreshold 2>/dev/null || echo "0")"
  if [[ "$current" == "0" ]]; then
    echo "Antialiasing: disabled"
  else
    echo "Antialiasing: ${current}"
  fi
}


antialiasthreshold(){
    if [[ "$1" =~ ^[0-9]+$ ]]; then
        defaults write NSGlobalDomain AppleAntiAliasingThreshold -int "${1}"
        echo "Antialiasing set to ${1}"
    else
        show_antialiasthreshold_status
    fi
}


iconsize(){
    local sidebar_icon_size
    local dock_icon_size
    local finder_icon_size

    case $1 in
        small)
            sidebar_icon_size="1"
            dock_icon_size="36"
            finder_icon_size="32"
            ;;
        medium)
            sidebar_icon_size="2"
            dock_icon_size="48"
            finder_icon_size="64"
            ;;
        large)
            sidebar_icon_size="3"
            dock_icon_size="96"
            finder_icon_size="128"
            ;;
        *)
            help
            ;;
    esac

    # sidebar icon size
    defaults write NSGlobalDomain NSTableViewDefaultSizeMode \
        -int "${sidebar_icon_size}" 2>/dev/null
    defaults write NSGlobalDomain NSNavPanelIconViewIconSizeForOpenMode \
        -int "${sidebar_icon_size}" 2>/dev/null

    # dock icon size
    defaults write com.apple.dock "tilesize" \
        -int "${dock_icon_size}" 2>/dev/null && killall Dock

    # Finder icon size
    defaults write com.apple.finder DesktopViewSettings.IconViewSettings.iconSize \
        -int 64 2>/dev/null && killall Finder
}


maincolor(){
    local color

    case $1 in
        blue)     color="1" ;;
        graphite) color="6" ;;
        *)        help ;;
    esac

    default write NSGlobalDomain AppleAquaColorVariant \
        -int "${color}" 2>/dev/null && killall SystemUIServer
}


highlightcolor(){
    local color

    # Highlight colors (from the crayons picker)
    case $1 in
        graphite)
            color="0.780400 0.815700 0.858800";;
        cayenne)
            color="0.501961 0.000000 0.000000";;
        asparagus)
            color="0.501961 0.501961 0.000000";;
        clover)
            color="0.000000 0.501961 0.000000";;
        teal)
            color="0.000000 0.501961 0.501961";;
        midnight)
            color="0.000000 0.000000 0.501961";;
        plum)
            color="0.501961 0.000000 0.501961";;
        tin)
            color="0.498039 0.498039 0.498039";;
        nickel)
            color="0.501961 0.501961 0.501961";;
        mocha)
            color="0.501961 0.250980 0.000000";;
        fern)
            color="0.250980 0.501961 0.000000";;
        moss)
            color="0.000000 0.501961 0.250980";;
        ocean)
            color="0.000000 0.250980 0.501961";;
        eggplant)
            color="0.250980 0.000000 0.501961";;
        maroon)
            color="0.501961 0.000000 0.250980";;
        steel)
            color="0.400000 0.400000 0.400000";;
        aluminum)
            color="0.600000 0.600000 0.600000";;
        maraschino)
            color="1.000000 0.000000 0.000000";;
        lemon)
            color="1.000000 1.000000 0.000000";;
        spring)
            color="0.000000 1.000000 0.000000";;
        turquoise)
            color="0.000000 1.000000 1.000000";;
        blueberry)
            color="0.000000 0.000000 1.000000";;
        magenta)
            color="1.000000 0.000000 1.000000";;
        iron)
            color="0.298039 0.298039 0.298039";;
        magnesium)
            color="0.701961 0.701961 0.701961";;
        tangerine)
            color="1.000000 0.501961 0.000000";;
        lime)
            color="0.501961 1.000000 0.000000";;
        seafoam)
            color="0.000000 1.000000 0.501961";;
        aqua)
            color="0.000000 0.501961 1.000000";;
        grape)
            color="0.501961 0.000000 1.000000";;
        strawberry)
            color="1.000000 0.000000 0.501961";;
        tungsten)
            color="0.200000 0.200000 0.200000";;
        silver)
            color="0.800000 0.800000 0.800000";;
        salmon)
            color="1.000000 0.400000 0.400000";;
        banana)
            color="1.000000 1.000000 0.400000";;
        flora)
            color="0.400000 1.000000 0.400000";;
        ice)
            color="0.400000 1.000000 1.000000";;
        orchid)
            color="0.400000 0.400000 1.000000";;
        bubblegum)
            color="1.000000 0.400000 1.000000";;
        lead)
            color="0.0980392 0.0980392 0.0980392";;
        mercery)
            color="0.901961 0.901961 0.901961";;
        cantaloupe)
            color="1.000000 0.800000 0.400000";;
        honeydew)
            color="0.800000 1.000000 0.400000";;
        spindrift)
            color="0.400000 1.000000 0.800000";;
        sky)
            color="0.400000 0.800000 1.000000";;
        lavender)
            color="0.800000 0.400000 1.000000";;
        carnation)
            color="1.000000 0.435294 0.811765";;
        licorice)
            color="0.000000 0.000000 0.000000";;
        snow)
            color="1.000000 1.000000 1.000000";;
        *)
            help ;;
    esac

    defaults write NSGlobalDomain AppleHighlightColor \
        -string "${color}" 2>/dev/null

    defaults write com.apple.systempreferences \
        AppleOtherHighlightColor \
        -string "${color}" 2>/dev/null

    echo "Highlight color set to ${1}"
}


option="${1}"; shift
case "${option}" in
    --help)
        help
        ;;
    --antialiasthreshold)
        antialiasthreshold "$@"
        ;;
    --darkmode)
        darkmode "$@"
        ;;
    --highlightcolor)
        highlightcolor "$@"
        ;;
    --iconsize)
        iconsize "$@"
        ;;
    --maincolor)
        maincolor "$@"
        ;;
    --transparency)
        transparency "$@"
        ;;
    *)
        help
        ;;
esac
